﻿using System.Diagnostics;

namespace MinefieldCheat
{
    internal class CCheatMapBuilder
    {
        private char[] _grid;
        private readonly int _height;
        private readonly int _width;

        public CCheatMapBuilder( int width, int height )
        {
            _width = width;
            _height = height;
            _grid = new char[width * height];

            for (int i = 0; i < height * width; i++)
                _grid[i] = '0';               
        }

        public void SetAt( int x, int y, int count )
        {
            Debug.Assert(x >= 0 && y >= 0 && x < _width && y <= _height);
            Debug.Assert(count < 9 && count >= 0 );

            // WARNING: highly inefficient code ahead!
            //var s = _grid[y].ToCharArray();
            //s[x] =  (char) (count+(int)'0');
            //_grid[y] = new string( s );

            _grid[_width * y + x] = (char)(count + (int)'0');
        }

        public string[] GetCheatMap()
        {
            string[] result = new string[ _height ];
            for (int y = 0; y < _height; y++ )
            {
                result[ y ] = new string( _grid , y*_width, _width );
            }
            return result;
        }

    }
}
